import React from 'react';
import PropTypes from 'prop-types';
import { Modal, Form, Input, Select, Button, message } from 'antd';
import { createProduct } from '@/api/product/index';

const { Option } = Select;

const AddProductModal = ({ visible, onClose, onSuccess }) => {
  const [form] = Form.useForm();


  const handleAddProduct = async (values) => {
    try {
      const res = await createProduct(values);
      if (res.status === 0) {
        message.success('产品入库成功');
        form.resetFields();
        onClose();
        onSuccess();
      } else {
        message.error('产品入库失败');
      }
    } catch (error) {
      message.error(error);
    }
  };

  return (
    <Modal
      title="添加产品入库"
      open={visible}
      onCancel={onClose}
      footer={null}
      width={600}
      destroyOnClose
    >
      <Form
        form={form}
        onFinish={handleAddProduct}
        layout="vertical"
        initialValues={{
          product_unit: '个',
        }}
      >
        <Form.Item
          name="product_id"
          label="入库编号"
          rules={[{ required: true, message: '请输入入库编号' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="product_name"
          label="产品名称"
          rules={[{ required: true, message: '请输入产品名称' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          name="product_category"
          label="产品类别"
          rules={[{ required: true, message: '请选择产品类别' }]}
        >
          <Select placeholder="请选择产品类别" options={[
            {
              label: '食品类',
              value: '食品类'
            },
            {
              label: '水果类',
              value: '水果类'
            }
          ]}>
          </Select>
        </Form.Item>

        <Form.Item
          name="product_unit"
          label="产品单位"
          rules={[{ required: true, message: '请选择产品单位' }]}
        >
          <Select placeholder="请选择产品单位">
            <Option value="个">个</Option>
            <Option value="件">件</Option>
          </Select>
        </Form.Item>

        <Form.Item
          name="product_in_warehouse_number"
          label="产品入库数量"
          rules={[{ required: true, message: '请输入产品入库数量' }]}
        >
          <Input type="number" />
        </Form.Item>

        <Form.Item
          name="product_single_price"
          label="产品入库单价"
          rules={[{ required: true, message: '请输入产品入库单价' }]}
        >
          <Input type="number" />
        </Form.Item>

        <Form.Item
          name="product_create_person"
          label="入库操作人"
          rules={[{ required: true, message: '请输入入库操作人' }]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          name="in_memo"
          label="入库备注"
        >
          <Input.TextArea rows={2} />
        </Form.Item>

        <Form.Item style={{ textAlign: 'right' }}>
          <Button type="primary" htmlType="submit">
            确定
          </Button>
        </Form.Item>
      </Form>
    </Modal >
  );
};

AddProductModal.propTypes = {
  visible: PropTypes.bool.isRequired,
  onClose: PropTypes.func.isRequired,
  onSuccess: PropTypes.func.isRequired,
};

export default AddProductModal;
